﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MBanner
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();

        public List<Banner> FindAll()
        {
            var v = from f in context.Banners select f;
            return v.ToList();
        }
        public Banner FindAll_Web()
        {
            var v = from f in context.Banners.Where(d => d.HienThi == true) select f;
            return v.FirstOrDefault();
        }

        public int GetTotalBanner()
        {
            return FindAll().Where(d => d.HienThi == true).Count();
        }

        public void Save(Banner sp)
        {
            context.Banners.InsertOnSubmit(sp);
            context.SubmitChanges();
        }
        public void Update(Banner bn)
        {
            var v = from f in context.Banners
                    select f;
            Banner onldb = v.SingleOrDefault(d => d.Id.Equals(bn.Id));
            onldb.UrlHinh = bn.UrlHinh;
            onldb.MaLoaiQuangCao = bn.MaLoaiQuangCao;
            context.SubmitChanges();
        }

        public void Delete(int id)
        {
            var v = from p in context.Banners
                    where p.Id.Equals(id)
                    select p;
            Banner spdb = v.SingleOrDefault();
            context.Banners.DeleteOnSubmit(spdb);
            context.SubmitChanges();
        }
        public void SaveChange()
        {
        }
        public void HienThi(int id)
        {
            var v = from f in context.Banners
                    select f;
            Banner spdb = v.SingleOrDefault(d => d.Id.Equals(id));
            if (spdb.HienThi == true)
            {
                spdb.HienThi = false;
            }
            else spdb.HienThi = true;
            context.SubmitChanges();
        }
        public Banner GetByID(int id)
        {
            return (FindAll().Where(d => d.Id == id)).SingleOrDefault();
        }


        public int GetMax()
        {
            return (from t in context.Banners
                    orderby t.Id descending
                    select t.Id).FirstOrDefault();
        }
    }
}